<template>
    <div class="container">
        <addMedicalProduct ref="addMedicalProduct" />
        <importFileView ref="importFileView" />
        <MedicalProductApply ref="MedicalProductApply" />
        <MedicalProductApplyCopy ref="MedicalProductApplyCopy" />
        <div class="main">
            <div class="header felx">
                <div class="header_l flex">
                    <div class="header_btn flex" @click="addHandle" style="margin-right: 30px;" v-if="flag">
                        <img src="../../assets/room_add_icon.png" style="width: 13px;height: 13px;margin-right: 0px;" />
                        <p>物资登记</p>
                    </div>
                    <div class="flex" style="cursor: pointer;margin-right: 40px;" v-if="flag"
                        v-loading.fullscreen.lock="fullscreenLoading" @click="importHandle">
                        <img src="../../assets/MedicalProductAssets_icon1.png">
                        <p>批量导入</p>
                    </div>
                    <div class="flex" style="cursor: pointer;" v-loading.fullscreen.lock="fullscreenLoading" v-if="flag"
                        @click="exportHandle">
                        <img src="../../assets/MedicalProductAssets_icon2.png">
                        <p>导出报表</p>
                    </div>
                </div>
                <div class="header_r flex">
                    <!-- <div class="header_btn flex" @click="print">
                        <img src="../../assets/MedicalProductAssets_icon3.png" style="width: 13px;height: 13px;">
                        <p>打印</p>
                    </div> -->
                    <img src="../../assets/MedicalProductAssets_icon3.png" style="width: 13px;height: 13px;">
                    <div class="header_btn flex" @click="applyAccets" v-if="false">
                        <p>物资申领</p>
                    </div>
                    <div class="serachBox flex">

                        <el-dropdown @command="selectHandle">
                            <div class="flex">
                                <p style="width: 70px;">{{ searchTitle }}</p> <el-icon>
                                    <ArrowDown />
                                </el-icon>
                            </div>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item command="物资名称">物资名称</el-dropdown-item>
                                    <!-- <el-dropdown-item command="物资编码">物资编码</el-dropdown-item> -->
                                    <el-dropdown-item command="物资分类">物资分类</el-dropdown-item>
                                    <el-dropdown-item command="上次供应商">上次供应商</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>

                        <input type="text" v-model="searchValue" @keyup.enter="searchEnter">
                        <button style="padding: 0 10px;" @click="getList">搜索</button>
                    </div>
                </div>
            </div>
            <div class="borderBox">
                <div class="borderTop">
                    <div class="assetsNum">物品数量：{{ total > 2000 ? "2000+" : total }}</div>
                    <div class="flex">
                        <!-- <div class="borderTop_btn">
                            <p>筛选条件</p>
                            <el-icon>
                                <ArrowDown />
                            </el-icon>
                        </div>
                        <div class="borderTop_btn">
                            <p>筛选条件</p>
                            <el-icon>
                                <ArrowDown />
                            </el-icon>
                        </div>
                        <div class="borderTop_btn">
                            <el-icon>
                                <ArrowDown />
                            </el-icon>
                            <p>切换视图</p>
                        </div> -->
                    </div>
                </div>
                <div class="table_box">
                    <el-table :data="list" border style="width: 100%;" scrollTo :row-class-name="tableRowClassName"
                        :header-cell-style="headerCellStyle" :row-style="tableRowStyle">
                        <el-table-column prop="categoriesName" label="分类名称" width="120">
                            <template #header>
                                <div class="header_icon">
                                    <span>分类名称</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="物资名称" width="180">
                            <template #header>
                                <div class="header_icon">
                                    <span>物资名称</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="stockQuantity" label="入库数量" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>入库数量</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="availableQuantity" label="可用数量" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>可用数量</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="specification" label="规格" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>规格</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit" label="单位" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>单位</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="warningThreshold" label="物资阈值" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>物资阈值</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column prop="code" label="流水号" width="140">
                            <template #header>
                                <div class="header_icon">
                                    <span>流水号</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column prop="nationalInsuranceCode" label="国家医保代码" width="140">
                            <template #header>
                                <div class="header_icon">
                                    <span>国家医保代码</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="nationalInsuranceName" label="国家医保名称" width="140">
                            <template #header>
                                <div class="header_icon">
                                    <span>国家医保名称</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column> -->
                        <!-- <el-table-column prop="chargeLevel" label="收费等级" width="140">
                            <template #header>
                                <div class="header_icon">
                                    <span>收费等级</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>  -->
                        <!-- <el-table-column prop="stockAmount" label="库存金额" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>库存金额</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column> -->
                        <el-table-column prop="model" label="型号" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>型号</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column prop="tenderCode" label="中标编码" width="160">
                            <template #header>
                                <div class="header_icon">
                                    <span>中标编码</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column> -->
                        <!-- <el-table-column prop="origin" label="产地" width="160">
                            <template #header>
                                <div class="header_icon">
                                    <span>产地</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column> -->
                        <el-table-column prop="warehouseBatch" label="库房分批" width="140">
                            <template #header>
                                <div class="header_icon">
                                    <span>库房分批</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column prop="lastPurchasePrice" label="上次采购价" width="120">
                            <template #header>
                                <div class="header_icon">
                                    <span>上次采购价</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="finalSellingPrice" label="最后售价" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>最后售价</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                
                        <el-table-column prop="stockPriceDifference" label="库存差价" width="100">
                            <template #header>
                                <div class="header_icon">
                                    <span>库存差价</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="lastSupplier" label="上次供应商" width="130">
                            <template #header>
                                <div class="header_icon">
                                    <span>上次供应商</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="createdAt" label="创建时间" width="120">
                            <template #header>
                                <div class="header_icon">
                                    <span>创建时间</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="updatedAt" label="更新时间" width="120">
                            <template #header>
                                <div class="header_icon">
                                    <span>更新时间</span>
                                    <el-icon class="rotate">
                                        <MoreFilled />
                                    </el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="120px" fixed="right">
                            <template style="display: block" #default="scope">
                                <el-button class="edit_btn" size="small" type="success" style="margin-right: 10px;"
                                    @click="editHandle(scope.row.id)">编辑
                                </el-button>
                                <el-button class="del_btn" size="small" type="danger"
                                    @click="delHandle(scope.row.id)">删除
                                </el-button>
                            </template>
                        </el-table-column>



                    </el-table>
                </div>

                <el-config-provider :locale="locale">
                    <div style="margin-top: 10px" class="pageBox flex">
                        <el-pagination :total="total" :current-page="page" :page-size="pageSize" background
                            layout="prev, pager, next, sizes" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                            @size-change="changePageSize" @current-change="changeCurrentPage" />
                        <div class="flex">
                            <p>第 {{ page }} 条</p>
                            <p>共 {{ total }} 条数据</p>
                        </div>
                    </div>
                </el-config-provider>
            </div>
        </div>
    </div>
</template>
<script>
import { httpService } from "@/utils/httpService";
import { formatTime } from "@/utils/dateFormat";
import { ElConfigProvider, ElMessage, ElMessageBox, ElLoading } from "element-plus";
import { MoreFilled, CaretLeft, CaretRight, ArrowLeftBold, ArrowRightBold, ArrowDown, Plus } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import addMedicalProduct from "../../components/medicalProduct/addMedicalProduct.vue"
import importFileView from "../../components/medicalProduct/importFileView.vue"
import MedicalProductApply from "../../components/medicalProduct/MedicalProductApply.vue"
import MedicalProductApplyCopy from "../../components/medicalProduct/MedicalProductApplyCopy.vue"

export default {
    name: "MedicalProductLogin",
    components: {
        ElMessage, MoreFilled, ElLoading, ElMessageBox, importFileView, addMedicalProduct, Plus, MedicalProductApplyCopy,
        CaretLeft, CaretRight, ArrowLeftBold, ArrowRightBold, ArrowDown, ElConfigProvider, MedicalProductApply,
    },
    data() {
        return {
            page: 1,
            pageSize: 10,
            total: 0,
            list: [],
            fullscreenLoading: false,
            searchTitle: "搜索条件",
            searchValue: "",
            flag: false,
        };
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    computed: {},
    mounted() {
        this.getMyPermission()
        this.getList()
    },
    methods: {
        searchEnter: function (e) {
            console.log(e);
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getList();
            }
        },
        getMyPermission() {
            httpService("/javaApi/getMyPermission", {}, "get").then(
                (data) => {
                    // if (data) {
                    //     if (data.includes("medicalProductPermission") ) {
                    //         this.flag = false
                    //     } else {
                    //         this.flag = true
                    //     } 
                    // }
                    if (data) {
                        if (data.includes("medicalProductPermission") && data.includes("everymedicalProductPermission")) {
                            this.flag = true
                        } else {
                            this.flag = false
                        }
                    }
                }
            );
        },
        tableRowClassName({ row, rowIndex }) {
            // console.log(rowIndex);
            return rowIndex % 2 === 1 ? 'warning-row' : '';
        },
        headerCellStyle() {
            return {
                background: '#f2f2f2',
            };
        },
        tableRowStyle({ row, rowIndex }) {
            if (rowIndex % 2 === 1) {
                return {
                    background: '#F5F5F5',
                };
            }
        },
        reload() {
            this.getList()
        },
        changePageSize(size) {
            this.pageSize = size;
            this.getList();
        },
        changeCurrentPage(page) {
            this.page = page;
            this.getList();
        },
        addHandle() {
            this.$refs.addMedicalProduct.flag = true
            this.$refs.addMedicalProduct.title = "物资登记"
            this.$refs.addMedicalProduct.getCategoryList()
        },
        applyAccets() {
            this.$refs.MedicalProductApply.flag = true
            this.$refs.MedicalProductApply.getDepartmentList()
            this.$refs.MedicalProductApply.getAssetsList()
            this.$refs.MedicalProductApply.getUserList()
            this.$refs.MedicalProductApply.getIdList()
        },
        print() {
            this.$refs.MedicalProductApplyCopy.flag = true

        },
        editHandle(id) {
            this.$refs.addMedicalProduct.flag = true
            this.$refs.addMedicalProduct.title = "修改物资"
            this.$refs.addMedicalProduct.queryById(id)
            this.$refs.addMedicalProduct.getCategoryList()
        },
        importHandle(id) {
            this.$refs.importFileView.flag = true
            this.$refs.importFileView.url = "/javaApi/materials/importExcel"
            this.$refs.importFileView.templateUrl = "/javaApi/materials/exportExcel"
        },
        selectHandle(value) {
            console.log(value);
            this.searchTitle = value
        },
        exportHandle() {
            this.fullscreenLoading = true
            httpService("/javaApi/materials/exportExcel", {}, "get").then(
                (data) => {
                    this.fullscreenLoading = false
                    if (data.code == 200) {
                        ElMessage.success("导出成功");
                        window.open(data.message, "_blank")
                    } else {
                        ElMessage.error("导出失败，请联系管理员!");
                    }
                }
            );
        },
        getList() {
            let obj = {}
            if (this.searchValue) {
                if (this.searchTitle == "搜索条件") {
                    ElMessage.error("请选择搜索条件");
                    return
                } else if (this.searchTitle == "物资名称") {
                    obj = { name: this.searchValue }
                } else if (this.searchTitle == "物资编码") {
                    obj = { code: this.searchValue }
                } else if (this.searchTitle == "物资分类") {
                    obj = { categoriesId: this.searchValue }
                } else if (this.searchTitle == "上次供应商") {
                    obj = { lastSupplier: this.searchValue }
                }
            } else {
                obj = {}
            }
            httpService(`/javaApi/materials/list/${this.page}/${this.pageSize}`, obj, "get").then((data) => {
                console.log(data);
                if (data) {
                    this.total = data.total
                    data.list.map(item => {
                        if (item.createdAt) {
                            item.createdAt = item.createdAt.slice(0, 10)
                        }
                        if (item.updatedAt) {
                            item.updatedAt = item.updatedAt.slice(0, 10)
                        }
                    })
                    this.list = data.list
                }
            });
        },
        delHandle(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/materials/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getList();
                            } else {
                                ElMessage.error("删除失败");
                            }
                        }
                    );
                })
                .catch(() => { });
        },
    },
};
</script>
<style scoped>
* {
    padding: 0;
    margin: 0;
}

.flex {
    display: flex;
    align-items: center;
}

.container {
    width: calc(100vw - 30px);
    height: calc(100vh - 30px);
    padding: 15px 15px;
    background-color: #EEF7FB;
}

.main {
    width: calc(100% - 30px);
    height: calc(100% - 30px);
    background-color: #fff;
    border-radius: 10px;
    padding: 15px;
}

.header {
    height: 45px;
    /* background-color: chocolate; */
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header_btn {
    width: 99px;
    height: 32px;
    background-color: #6F72FF;
    border-radius: 5px;
    justify-content: center;
    font-size: 14px;
    color: #fff;
    margin-right: 15px;
    cursor: pointer;
}

.header_btn img {
    width: 17px;
    height: 18px;
    margin-right: 5px;
}

.serachBox {
    color: #666666;
    font-size: 14px;
    width: 340px;
    height: 36px;
    border: 1px solid #D0D0D0;
    box-sizing: border-box;
    border-radius: 20px;
}

.serachBox .flex {
    justify-content: center;
    border-right: 1px solid #D0D0D0;
    margin-right: 10px;
    padding: 0 5px 0 15px;
    color: #666666;
    cursor: pointer;
}

.serachBox input {
    height: calc(100% - 4px);
    border: none;
    flex: 1;
    outline: none;
}

.serachBox button {
    width: 80px !important;
    height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #6F72FF;
    color: #fff;
    font-size: 14px;
    margin-right: 2px;
    outline: none;
}


.borderBox {
    height: calc(100% - 60px);
    border: 1px solid #DFDFDF;
    border-radius: 5px;
    box-sizing: border-box;
    margin-top: 15px;
}

.borderTop {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.assetsNum {
    margin-left: 20px;
    color: #666666;
    font-size: 14px;
}

.borderTop .flex {
    margin-right: 20px;
}

.borderTop_btn {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #666666;
    margin-left: 40px;
    cursor: pointer;
}

.header_icon {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.table_box {
    width: calc(100vw - 283px);
    height: calc(100% - 93px);
    /* background-color: slategrey; */
    overflow-y: auto;
}

.el-table {
    /* margin-left: -1px; */
}

.rotate {
    transform: rotate(90deg);
}


.el-scrollbar__bar {
    z-index: 11;
    overflow-x: hidden;
    position: sticky;
}

.el-table.el-table--border {
    overflow: unset;
}


.pageBox {
    background-color: #f6f6f6;
    padding: 5px 0;
    border-top: 1px solid #e7e7e7;
    border-radius: 0 0 5px 5px;
}

.el-pagination {
    margin-left: 10px;
}

.pageBox {
    justify-content: space-between;
}

.pageBox .flex {
    color: #666;
    font-size: 14px;
    margin-right: 10px;
}

.pageBox .flex p {
    color: #666;
    font-size: 14px;
    margin-right: 20px;
}

.edit_btn {
    background-color: transparent;
    color: #007EFF;
    border: none;
}

.del_btn {
    background-color: transparent;
    color: #FF0000;
    border: none;
}

.header_l .flex p {
    margin: 0 5px;
    white-space: nowrap;
}

.el-tooltip__trigger:focus-visible {
    outline: unset;
}
</style>